

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Person;
import db.ProjectDAO;
import db.SkillDAO;
import db.TaskDAO;

public class PmServlet extends HttpServlet {

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		Person p = (Person)req.getSession().getAttribute("person");
		List projectTitles = Dao.getManagedProjectTitles(p);
		req.setAttribute("projectTitles", projectTitles);
		getServletContext().getRequestDispatcher("/pm-home.jsp").forward(req, resp);
	}
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		int projectId = Integer.parseInt(req.getParameter("projectId"));
		
		String action = req.getParameter("act");
		if("addTask".equals(action)) {
			int sequence = Integer.parseInt(req.getParameter("sequence"));
			String description = req.getParameter("description");
			ProjectDAO.addTask(projectId, sequence, description);
		} else if("assignSkillToTask".equals(action)) {
			int taskId = Integer.parseInt(req.getParameter("taskId"));
			int skillId = Integer.parseInt(req.getParameter("skillId"));
			String startDate = req.getParameter("startDate");
			String endDate = req.getParameter("endDate");
			int percent = Integer.parseInt(req.getParameter("percent"));
			int level = Integer.parseInt(req.getParameter("level"));
			TaskDAO.addSkill(taskId, skillId, level, startDate, endDate, percent);
		}
		
		List people = Dao.getPeopleAssignedToProject(projectId);
		List metrics = Dao.getTaskMetricsForProject(projectId);
		List skills = SkillDAO.getSkills();
		req.setAttribute("projectId", new Integer(projectId));
		req.setAttribute("people", people);
		req.setAttribute("metrics", metrics);
		req.setAttribute("skills", skills);
		getServletContext().getRequestDispatcher("/pm-view-project.jsp").forward(req, resp);
	}
	
	
	
}
